<?php

if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * statiticsHr controller
 * @author Phuc Nguyen - 09/03/2015
 * http://www.facebook.com/phucthuong
 * email : nguyenvanphuc0626@gmail.com
*/
class StatiticsHr extends BACKEND_Controller {

	public function __construct() {
		parent::__construct();
		$this->load->language('statiticsHr');
		$this->load->language('button');
		if($this->database_connect_status){
			$this->load->model('statistic_model');
			$this->set_controller('statiticsHr');
			$this->set_model($this->statistic_model);
		}
                
                $this->load->library('bookinglib');
                $this->booking = new bookinglib();
	}
        
        private function menuTop(){
            $this->load->model('stats_model');
            $user_info                          = $this->stats_model->get_user_info($this->stats_model->get_user_id());
            $this->view_data['role_by_group']   = $user_info['role_list'];
            $this->view_data['username']        = $user_info['username'];
            return $this->view_data;
        }
        
        public function lists(){
            $this->menuTop();
            if($this->input->server('REQUEST_METHOD')=='POST'){
                $beginTime                      = ($this->input->post('beginTime'))?date('Y-m-d',strtotime($this->input->post('beginTime'))):'';
                $endTime                        = ($this->input->post('endTime'))?date('Y-m-d',strtotime($this->input->post('endTime'))):'';
                $this->view_data['lists']       = $this->statistic_model->statiticUsers($beginTime,$endTime);
                
               //export data to excel
               if($this->input->post('submit') == 'export'){
                   
                    $lists = array();
                    foreach($this->view_data['lists'] as $vals){
                         $lists[] = array(
                            'username'          =>  strtoupper($this->booking->convertStringAsUrl($vals->firstname)),
                            'phone_calltime'    =>  $vals->phone_calltime,
                            'phone_busy'        =>  $vals->phone_busy,
                            'phone_limited'     =>  $vals->phone_limited,
                            'callback'          =>  $vals->callback,
                            'callok'            =>  $vals->callok
                         );
                    }
                    
                    header("Content-Disposition: attachment; filename=\"danh-sach-salers-".date('d-m-Y').".xls\"");
                    header("Content-Type: application/vnd.ms-excel;");
                    header("Pragma: no-cache");
                    header("Expires: 0");
                    $out        = fopen("php://output", 'w');
                    echo "\nNhan vien\t Thoi gian goi(s)\t Khong nghe may\t Khong lien lac duoc\t Goi lai sau\t Goi thanh cong\n";
                    foreach ($lists as $data)
                    {
                        fputcsv($out, $data,"\t");
                    }
                    fclose($out);
                    unset($this->view_data['lists']);
                    unset($lists);
                    die("");
               }
            } else {
                $this->view_data['lists']     = $this->statistic_model->statiticUsers(date('Y-m-d'),date('Y-m-d'));
            }
            $this->load->view('auth/statiticsHr/lists', $this->view_data);
        }
}